package cn.com.paycn.ken.service.impl;

import java.util.Date;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import cn.com.paycn.ken.dao.BaseDao;
import cn.com.paycn.ken.hbm.LoginLog;
import cn.com.paycn.ken.hbm.LoginUser;
import cn.com.paycn.ken.hbm.Member;
import cn.com.paycn.ken.hbm.Role;
import cn.com.paycn.ken.service.LoginUserService;
import cn.com.paycn.ken.utils.Encrypt;
import cn.com.paycn.ken.utils.IpUtils;
import cn.com.paycn.ken.utils.ResourceUtils;
import cn.com.paycn.ken.utils.exception.LoginException;


@Component
@Transactional
public class LoginUserServiceImpl implements LoginUserService {

	@Autowired
	private BaseDao baseDao;

	public LoginUserServiceImpl()
    {
    }

    public Member login(LoginUser user, HttpServletRequest request)
        throws LoginException
    {
        Member member = null;
        try
        {
            member = null;//loginAuth(user);
           // loginLog(request, StaticParamService.LoginLog.getStatusSuccesss());
        }
        catch(Exception e)//LoginException e)
        {
          //  loginLog(request, StaticParamService.LoginLog.getStatusError());
            throw new LoginException(e.getMessage());
        }
        return member;
    }

    private Member loginAuth(LoginUser user)
        throws LoginException
    {
        Object userObj = baseDao.get("from LoginUser where username = ? and password = ?", new Object[] {
            user.getUsername(), Encrypt.md5AndSha(user.getPassword())
        });
        Member member = null;
        if(userObj instanceof LoginUser)
        {
            LoginUser loginUser = (LoginUser)userObj;
            Object memberObj = baseDao.get("select m from Member m join fetch m.roles r where m.username = ? and r.type = ?", new Object[] {
                loginUser.getUsername(), ResourceUtils.SYSTEM_TYPE
            });
            if(memberObj instanceof Member)
            {
                member = (Member)memberObj;
                if(!StaticParamService.Member.getStatusActive().equals(member.getStatus()))
                    throw new LoginException("\u7528\u6237\u4E0D\u53EF\u7528");
                if(CollectionUtils.isNotEmpty(member.getRoles()))
                {
                    for(Iterator iterator = member.getRoles().iterator(); iterator.hasNext();)
                    {
                        Role r = (Role)iterator.next();
                        if(ResourceUtils.SYSTEM_TYPE.equals(r.getType()))
                            return member;
                    }

                }
                throw new LoginException("\u7528\u6237\u6743\u9650\u4E0D\u8DB3");
            } else
            {
                throw new LoginException("\u7528\u6237\u4FE1\u606F\u7F3A\u5931");
            }
        } else
        {
            throw new LoginException("\u7528\u6237\u540D\u6216\u5BC6\u7801\u6709\u8BEF");
        }
    }

    private void loginLog(HttpServletRequest request, String status)
    {
        LoginLog log = new LoginLog();
        log.setUsername(request.getParameter("username"));
        log.setStatus(status);
        log.setIp(IpUtils.ipAddress(request));
        log.setAgent(request.getHeader("user-agent"));
        log.setLoginDate(new Date());
        baseDao.save(log);
    }
}
